在 Ruby 中,輸入/輸出不僅僅是一組指令;它是一種複雜的 基於流的架構。此系統的核心是 IO 類別,它作為一個 雙向通道 在您的程式與外部世界之間。無論您是在與檔案、網路插座或使用者終端機互動,Ruby 都將它們視為通用的資料流。
1. 雙向橋樑
一個 IO 物件是一種統一的抽象。雖然作業系統會區分 檔案描述符 用於讀取和寫入(例如在管道中),Ruby 會將這些封裝成單一物件。這允許資料在兩個方向上無縫流動。
2. 核心模組與標準過濾器
這個 核心 模組提供像 gets 和 print。這些基本上是 過濾器 ,它們會委派給全域常數 STDIN 和 STDOUT。這表示您的程式碼可以在作業系統層級被重新導向,以處理檔案或網路資料流,而無需更改任何一行邏輯。
$$\text{資料流} \rightarrow \text{緩衝區} \rightarrow \text{Ruby 解譯器}$$
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary role of the Ruby
IO class?To convert Ruby objects into JSON strings.
To act as a bidirectional channel between Ruby and external resources.
To manage CPU thread cycles.
To define the graphical user interface of an application.
✅ Correct!
Correct! The IO class provides a unified interface for streams like files, pipes, and sockets.❌ Incorrect
The IO class specifically manages communication channels, not data serialization or threading.QUESTION 2
Which Kernel method reads an entire stream into an array of lines?
getsreadlinereadlinesread_all✅ Correct!
Correct! readlines consumes the entire stream and returns an array where each element is a line.❌ Incorrect
gets and readline only fetch a single line at a time.QUESTION 3
What is the difference between
print and puts?print adds a newline; puts does not.puts adds a record separator (newline) to the output; print does not.puts is used for files; print is for the screen.There is no difference; they are aliases.
✅ Correct!
Correct! puts is a line-oriented output method that ensures a trailing newline.❌ Incorrect
Actually, it's the other way around: puts appends the newline.QUESTION 4
Which constant represents the default standard input in Ruby?
$INPUT
STDIN
IO_IN
CONSOLE
✅ Correct!
Correct! STDIN is the global constant referring to the standard input IO object.❌ Incorrect
STDIN is the standard Ruby constant for input streams.QUESTION 5
What does the
<< operator do when used with an IO object like STDOUT?It shifts bits to the left.
It pushes data into the output stream without adding newlines or separators.
It creates a new subprocess.
It closes the stream.
✅ Correct!
Correct! The shovel operator (<<) treats the IO object like a buffer, appending data directly.❌ Incorrect
While << is a bit-shift operator for Integers, for IO objects it is the stream append operator.Case Study: The Standard Filter Pattern
Applying IO Abstractions in CLI Tools
You are tasked with creating a CLI tool that transforms text. The tool should read lines from the standard input, prepend a timestamp to each line, and output them to standard output. This tool must work both interactively and when redirected from a file (e.g., `cat log.txt | ruby transform.rb`).
Q
1. Why is the `gets` method suitable for this task regardless of whether the input is a keyboard or a file?
Solution:
Because `gets` is a Kernel method that defaults to reading from `STDIN`. In Ruby's stream architecture, `STDIN` is an `IO` object that abstracts the source; the program doesn't need to know if the underlying file descriptor points to a TTY or a disk file.
Because `gets` is a Kernel method that defaults to reading from `STDIN`. In Ruby's stream architecture, `STDIN` is an `IO` object that abstracts the source; the program doesn't need to know if the underlying file descriptor points to a TTY or a disk file.
Q
2. If you needed to output a single character to the stream without any formatting, which method would you use?
Solution:
The `putc` method is designed specifically for outputting a single character to the stream.
The `putc` method is designed specifically for outputting a single character to the stream.
Q
3. How does the `STDOUT << data` syntax differ from `puts data` in a multithreaded context based on the lesson principles?
Solution:
`puts` is non-atomic because it writes the data and then writes a newline as separate operations. Using `<<` also writes raw data, but one must be careful with interleaving; however, the primary difference is that `<<` does not automatically add formatting or newlines, providing raw stream access.
`puts` is non-atomic because it writes the data and then writes a newline as separate operations. Using `<<` also writes raw data, but one must be careful with interleaving; however, the primary difference is that `<<` does not automatically add formatting or newlines, providing raw stream access.